<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>浏览器自动化测试工具</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            font-family: "Microsoft YaHei", sans-serif;
            padding: 15px;
            background-color: #f8f9fa;
            min-width: 320px;
        }
        .header {
            margin-bottom: 20px;
            position: sticky;
            top: 0;
            background-color: #f8f9fa;
            z-index: 100;
            padding-bottom: 10px;
            border-bottom: 1px solid #dee2e6;
        }
        .script-list {
            max-height: 300px;
            overflow-y: auto;
            margin-bottom: 20px;
        }
        .script-item {
            cursor: pointer;
            padding: 8px 12px;
            margin-bottom: 5px;
            border-radius: 4px;
            background-color: #fff;
            border: 1px solid #dee2e6;
            transition: all 0.2s;
        }
        .script-item:hover {
            background-color: #f1f3f5;
        }
        .script-item.active {
            background-color: #e9ecef;
            border-color: #adb5bd;
        }
        .step {
            padding: 8px;
            margin-bottom: 5px;
            border: 1px solid #dee2e6;
            border-radius: 4px;
            background-color: #fff;
        }
        .step-header {
            display: flex;
            justify-content: space-between;
            cursor: pointer;
        }
        .step-body {
            margin-top: 8px;
            display: none;
        }
        .step-body.show {
            display: block;
        }
        .tab-content {
            padding-top: 15px;
        }
        .log-container {
            max-height: 200px;
            overflow-y: auto;
            background-color: #212529;
            color: #f8f9fa;
            font-family: monospace;
            padding: 10px;
            border-radius: 4px;
        }
        .log-entry {
            margin-bottom: 5px;
            white-space: pre-wrap;
            word-break: break-word;
        }
        .log-info {
            color: #8be9fd;
        }
        .log-success {
            color: #50fa7b;
        }
        .log-error {
            color: #ff5555;
        }
        .log-warning {
            color: #ffb86c;
        }
        .progress-container {
            margin: 15px 0;
        }
        .form-label {
            font-weight: 500;
        }
        .screenshot-container {
            text-align: center;
            margin-top: 15px;
        }
        .screenshot-container img {
            max-width: 100%;
            border: 1px solid #dee2e6;
            border-radius: 4px;
        }
        #add-step-btn {
            position: sticky;
            bottom: 10px;
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="header">
        <h3 class="mb-3">浏览器自动化测试工具</h3>
        <ul class="nav nav-tabs" id="myTab" role="tablist">
            <li class="nav-item" role="presentation">
                <button class="nav-link active" id="run-tab" data-bs-toggle="tab" data-bs-target="#run-tab-pane" type="button" role="tab" aria-controls="run-tab-pane" aria-selected="true">运行</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="edit-tab" data-bs-toggle="tab" data-bs-target="#edit-tab-pane" type="button" role="tab" aria-controls="edit-tab-pane" aria-selected="false">编辑</button>
            </li>
            <li class="nav-item" role="presentation">
                <button class="nav-link" id="log-tab" data-bs-toggle="tab" data-bs-target="#log-tab-pane" type="button" role="tab" aria-controls="log-tab-pane" aria-selected="false">日志</button>
            </li>
        </ul>
    </div>

    <div class="tab-content" id="myTabContent">
        <!-- 运行脚本标签页 -->
        <div class="tab-pane fade show active" id="run-tab-pane" role="tabpanel" aria-labelledby="run-tab" tabindex="0">
            <div class="mb-3">
                <label class="form-label">选择脚本：</label>
                <div class="script-list" id="script-list"></div>
            </div>
            
            <div id="script-detail" style="display:none;">
                <div class="mb-3">
                    <h5 id="selected-script-name">脚本名称</h5>
                    <p id="selected-script-desc" class="text-muted">脚本描述</p>
                </div>
                
                <div id="param-container" class="mb-3">
                    <!-- 参数输入区域将动态生成 -->
                </div>
                
                <div class="progress-container" id="progress-container" style="display:none;">
                    <div class="progress" style="height: 20px;">
                        <div id="progress-bar" class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
                    </div>
                    <div id="progress-status" class="mt-2 text-center"></div>
                </div>
                
                <div class="d-grid gap-2">
                    <button class="btn btn-primary" id="run-script-btn">运行脚本</button>
                    <button class="btn btn-danger" id="stop-script-btn" style="display:none;">停止脚本</button>
                </div>
                
                <div class="screenshot-container" id="screenshot-container" style="display:none;">
                    <h5>最新截图</h5>
                    <img id="screenshot-img" src="" alt="截图">
                </div>
            </div>
        </div>
        
        <!-- 编辑脚本标签页 -->
        <div class="tab-pane fade" id="edit-tab-pane" role="tabpanel" aria-labelledby="edit-tab" tabindex="0">
            <form id="script-form">
                <div class="mb-3">
                    <label for="script-name" class="form-label">脚本名称</label>
                    <input type="text" class="form-control" id="script-name" required>
                </div>
                
                <div class="mb-3">
                    <label for="script-desc" class="form-label">脚本描述</label>
                    <textarea class="form-control" id="script-desc" rows="2"></textarea>
                </div>
                
                <div class="mb-3">
                    <label for="script-params" class="form-label">参数定义 (JSON格式)</label>
                    <textarea class="form-control" id="script-params" rows="3" placeholder='{"url": "网址参数", "keyword": "搜索关键词"}'></textarea>
                    <div class="form-text">格式: {"参数名": "参数描述", ...}</div>
                </div>
                
                <hr>
                <h5>步骤列表</h5>
                <div id="steps-container">
                    <!-- 步骤内容将动态生成 -->
                </div>
                
                <button type="button" id="add-step-btn" class="btn btn-outline-primary mt-3">
                    <i class="bi bi-plus-circle"></i> 添加步骤
                </button>
                
                <div class="d-grid gap-2 mt-3">
                    <button type="submit" class="btn btn-success">保存脚本</button>
                </div>
            </form>
        </div>
        
        <!-- 日志标签页 -->
        <div class="tab-pane fade" id="log-tab-pane" role="tabpanel" aria-labelledby="log-tab" tabindex="0">
            <div class="mb-2 d-flex justify-content-between align-items-center">
                <h5 class="mb-0">执行日志</h5>
                <button class="btn btn-sm btn-outline-secondary" id="clear-log-btn">清除日志</button>
            </div>
            <div class="log-container" id="log-container">
                <!-- 日志内容将动态生成 -->
            </div>
        </div>
    </div>
    
    <!-- 步骤模板 (用于JS克隆) -->
    <template id="step-template">
        <div class="step mb-3" data-step-index="${index}">
            <div class="step-header">
                <div>
                    <span class="step-index">${index + 1}</span>. 
                    <span class="step-action">${action}</span>
                </div>
                <div>
                    <button type="button" class="btn btn-sm btn-outline-danger remove-step-btn">删除</button>
                    <button type="button" class="btn btn-sm btn-outline-secondary toggle-step-btn">展开</button>
                </div>
            </div>
            <div class="step-body">
                <div class="mb-2">
                    <label class="form-label">操作类型</label>
                    <select class="form-select step-action-select">
                        <option value="navigate_to_url">打开网址</option>
                        <option value="click">点击元素</option>
                        <option value="input">输入文本</option>
                        <option value="wait">等待时间</option>
                        <option value="extract">提取内容</option>
                        <option value="execute_js">执行JS</option>
                        <option value="screenshot">屏幕截图</option>
                    </select>
                </div>
                
                <div class="step-args">
                    <!-- 参数内容将根据选择的操作类型动态生成 -->
                </div>
                
                <div class="mb-2">
                    <label class="form-label">延迟执行(毫秒)</label>
                    <input type="number" class="form-control step-delay" min="0" value="0">
                </div>
            </div>
        </div>
    </template>
    
    <!-- 步骤参数模板 -->
    <template id="args-navigate_to_url-template">
        <div class="mb-2">
            <label class="form-label">URL</label>
            <input type="text" class="form-control" name="url" placeholder="${url}">
            <div class="form-text">支持参数: ${param}</div>
        </div>
    </template>
    
    <template id="args-click-template">
        <div class="mb-2">
            <label class="form-label">选择器</label>
            <input type="text" class="form-control" name="selector" placeholder="例如: #submit-button, .product-item">
        </div>
    </template>
    
    <template id="args-input-template">
        <div class="mb-2">
            <label class="form-label">选择器</label>
            <input type="text" class="form-control" name="selector" placeholder="例如: #search-input, [name='username']">
        </div>
        <div class="mb-2">
            <label class="form-label">文本内容</label>
            <input type="text" class="form-control" name="text" placeholder="要输入的文本，支持参数: ${param}">
        </div>
    </template>
    
    <template id="args-wait-template">
        <div class="mb-2">
            <label class="form-label">等待时间(毫秒)</label>
            <input type="number" class="form-control" name="milliseconds" min="0" value="1000">
        </div>
    </template>
    
    <template id="args-extract-template">
        <div class="mb-2">
            <label class="form-label">选择器</label>
            <input type="text" class="form-control" name="selector" placeholder="例如: .product-price, h1">
        </div>
    </template>
    
    <template id="args-execute_js-template">
        <div class="mb-2">
            <label class="form-label">JavaScript代码</label>
            <textarea class="form-control" name="code" rows="4" placeholder="// 编写要执行的JavaScript代码"></textarea>
        </div>
    </template>
    
    <template id="args-screenshot-template">
        <div class="mb-2">
            <p class="form-text">截取当前页面的可见区域</p>
        </div>
    </template>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script src="panel.js"></script>
</body>
</html> 